import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @Author zqc
 * @Date 2021/1/25 10:58
 * @Description 启动线程的五种方式
 */
public class HowToCreateThread {
    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("Hello MyThread!");
        }
    }

    static class MyRun implements Runnable {
        @Override
        public void run() {
            System.out.println("Hello MyRunnable!");
        }
    }

    static class MyCall implements Callable<String> {
        @Override
        public String call() {
            System.out.println("Hello MyCallable");
            return "success";
        }
    }

    //启动线程的5种方式
    public static void main(String[] args) {
        //① 标准1
        new MyThread().start();

        //② 标准2
        new Thread(new MyRun()).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello MyRun2!");
            }
        });

        //③ Java8新特性-Lambda表达式
        new Thread(()->{
            System.out.println("Hello Lambda!");
        }).start();

        //④ 线程池1
        Thread t = new Thread(new FutureTask<String>(new MyCall()));
        t.start();

        //⑤ 线程池2
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(()->{		//开启线程池
            System.out.println("Hello ThreadPool");
        });
        service.shutdown();			//关闭线程池
    }

}